package o;

import android.util.Log;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.ImageHeaderParser;
import com.bumptech.glide.load.ImageHeaderParser.ImageType;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.List;

public class xy implements se<InputStream, xt> {
    private final ud ˊ;
    private final List<ImageHeaderParser> ˋ;
    private final se<ByteBuffer, xt> ˎ;

    public /* synthetic */ boolean ˊ(@NonNull Object obj, @NonNull sc scVar) throws IOException {
        return ॱ((InputStream) obj, scVar);
    }

    public /* synthetic */ tx ˎ(@NonNull Object obj, int i, int i2, @NonNull sc scVar) throws IOException {
        return ˋ((InputStream) obj, i, i2, scVar);
    }

    public xy(List<ImageHeaderParser> list, se<ByteBuffer, xt> seVar, ud udVar) {
        this.ˋ = list;
        this.ˎ = seVar;
        this.ˊ = udVar;
    }

    public boolean ॱ(@NonNull InputStream inputStream, @NonNull sc scVar) throws IOException {
        return !((Boolean) scVar.ॱ(xx.ˏ)).booleanValue() && rx.ˏ(this.ˋ, inputStream, this.ˊ) == ImageType.GIF;
    }

    public tx<xt> ˋ(@NonNull InputStream inputStream, int i, int i2, @NonNull sc scVar) throws IOException {
        byte[] ˋ = ˋ(inputStream);
        if (ˋ == null) {
            return null;
        }
        return this.ˎ.ˎ(ByteBuffer.wrap(ˋ), i, i2, scVar);
    }

    private static byte[] ˋ(InputStream inputStream) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(16384);
        try {
            byte[] bArr = new byte[16384];
            while (true) {
                int read = inputStream.read(bArr);
                if (read != -1) {
                    byteArrayOutputStream.write(bArr, 0, read);
                } else {
                    byteArrayOutputStream.flush();
                    return byteArrayOutputStream.toByteArray();
                }
            }
        } catch (Throwable e) {
            if (Log.isLoggable("StreamGifDecoder", 5)) {
                Log.w("StreamGifDecoder", "Error reading data from stream", e);
            }
            return null;
        }
    }
}
